<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>DCC 矿机</title>

  <link rel="stylesheet" href="../static/css/font-awesome.min.css">
  <link rel="stylesheet" href="../static/css/style.css">
  <link rel="stylesheet" href="../static/css/wallets.css">
  <link rel="stylesheet" href="../static/css/send.css">
  <link rel="stylesheet" href="../static/css/common.css">
  <link rel="stylesheet" href="../static/css/jquery.nstSlider.css">
  <link rel="stylesheet" href="../static/css/zeroModal.css">
  <link rel="stylesheet" href="../static/css/accountDetail.css">
  <link rel="stylesheet" href="../static/css/toast.style.css">
  <link rel="stylesheet" href="../static/css/perfect-scrollbar.css">
  <link rel="stylesheet" href="../static/css/loading.css">
  <link rel="stylesheet" href="../static/css/window.css">
</head>

<body>
  <div class="afterLogin">
    <!-- 头部导航栏 -->
    <nav>
      <div id="wallets">
        <p>
          <i class="fa fa-credit-card-alt" aria-hidden="true"></i>
        </p>
        <span>钱包</span>
      </div>
      <div id="send">
        <p>
          <i class="fa fa-arrow-circle-o-up" aria-hidden="true"></i>
        </p>
        <span>交易</span>
      </div>
      <div id="contract">
        <p>
          <i class="fa fa-codepen" aria-hidden="true"></i>
        </p>
        <span>合约</span>
      </div>
      <div>
        <div>DC 总币量: </div>
        <div>
          <span id="balance" class="counter">0.00</span>
          <span class="cursor">DC</span>
        </div>
      </div>
      <div>
        <p class="cursor">MAIN NET</p>
        <span>
          <span>
            <i class="fa fa-share-alt" aria-hidden="true"></i>
          </span>
          <span id="node-count" style="-webkit-app-region: no-drag;" title="用户节点">0</span>
        </span>
        &nbsp;&nbsp;
        <span>
          <span>
            <i class="fa fa-linode" aria-hidden="true"></i>
          </span>
          <span id="block-number" style="-webkit-app-region: no-drag;" title="区块高度">0</span>
        </span>
        <span class="wd-button">
          <button id="start">开始挖矿</button>
          <button id="stop">停止挖矿</button>
        </span>
        <span>
          <div id="preloader_1">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            <!-- <span></span> -->
          </div>
        </span>
        <span class="wd-tip" style="font-size: 11px;
      color: #fff;"></span>
      </div>
      <!-- <div>
      
      
    </div> -->
    </nav>

    <div class="window">
      <div title="退出" onclick="javascript:window.location.href='./login.html'">
        <i class="fa fa-power-off" aria-hidden="true"></i>
      </div>
      <div id="minbt" title="最小化">
        <i class="fa fa-window-minimize" aria-hidden="true"></i>
      </div>
      <div id="maxbt" title="最大化">
        <i class="fa fa-window-maximize" aria-hidden="true"></i>
      </div>
      <div id="closebt" title="关闭">
        <i class="fa fa-times" aria-hidden="true"></i>
      </div>
    </div>

    <style>
      .wd-tip {
        display: inline-block;
        margin-left: 60px;
      }

      .wd-button {
        margin-left: 30px;
      }

      .wd-button button {
        margin-left: 10px;
        background-color: #827A7A;
        padding: 2px;
        color: #fff;
        border-radius: 2px;
        -webkit-app-region: no-drag;
        cursor: pointer;
      }

      .wd-button button:nth-child(1) {
        margin-left: 10px;
        background-color: #C20E25;
        padding: 2px;
        color: #fff;
        border-radius: 2px;
      }

      .all {
        width: 100%;
        position: absolute;
        height: calc(100% - 86px);
        overflow: auto;
        margin-top: 86px;
      }

      .nstSlider {
        height: 5px;
        margin-top: 20px;
        margin-bottom: 15px !important;
        background-color: #CCC6C6 !important;
      }

      .leftGrip {
        width: 15px !important;
        height: 15px !important;
        top: -5px;
        background-color: #02A8F3 !important;
      }
    </style>
    <style>
    </style>
    <div id="container" class="all">
      <!-- 内容 width:90%; 居中 -->
      <div class="content nano">
        <div id="all-wallets-content" class="nano-content">
          <div class="wallets title">
            <p>
              <span>账户</span>
              <span>概述</span>
            </p>
            <span class="sign">关于帐号</span>
            <p class="sign-content" style="margin-bottom: 0px;">帐户是密码保护的密钥，你可以持有DC币，且安全地基于电子的代币或硬币和控制合同。另外，帐户不能显示传入的事务。
            </p>
          </div>
          <div class="send-info">
            <div>
              <div></div>
              <div>
                <input id="search-account-detail" style="font-size: 13px; margin-bottom: 15px;" type="text" placeholder="输入邮箱地址，搜索用户钱包详情…">
              </div>
            </div>
          </div>
          <div class="all-account">

          </div>
          <div>
            <span class="sign">钱包合约</span>
            <p class="sign-content">一旦你拥有超过1个DC，你就可以创造更复杂的合约。钱包是一种基本的智能合约，它允许你的资金被多个帐户控制，并有一个可选的每日取款限额，以增加安全性。要创建您自己的自定义合约，请转到
              <合约>。</p>
            <!-- <div style="margin-top:20px; width: 200px;">
            <div id="add-account-contract" class="add-account">
              <div>
                <i class="fa fa-plus" aria-hidden="true"></i>
              </div>
              <div>添加钱包合约</div>
            </div>
          </div> -->
            <br>
          </div>
          <div class="trade-recode">
            <span class="sign">交易记录</span>
            <p class="sign-content">所有帐号最新交易记录，所有帐号均可看到。
            </p>
            <div class="send-info">
              <div>
                <div></div>
                <div>
                  <input id="search-trade" style="font-size: 13px" type="text" placeholder="输入交易hash，搜索交易详情…">
                </div>
              </div>
            </div>
            <div class="record-list">

              <div class="get-more">
                <p>查看更多</p>
              </div>
            </div>

          </div>
        </div>
        <div id="all-send-content" class="nano-content">
          <div class="title wallets">
            <p>
              <span>交易</span>
              <span>信息</span>
            </p>
          </div>
          <div class="send-info">
            <div>
              <div>From</div>
              <div>
                <input id="from" type="text" placeholder="0x000000…">
              </div>
            </div>
            <div>
              <div>To</div>
              <div>
                <input id="to" type="text" placeholder="0x000000…">
              </div>
            </div>
          </div>
          <div class="amount">
            <div>
              <div>数量</div>
              <div>
                <input id="amount" type="text" placeholder="0.00" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
              </div>
            </div>
            <div>
              <span>DC</span>
              <span style="text-align: right;">
                <span>共有</span>
                <span id="t_amount">- -</span>
                <span>DC</span>
                <p style="text-align: right; margin-top: -30px; font-size: 11px;">
                  <span>其中</span>
                  <span id="freeze_amount">- -</span>
                  <span>DC
                    <span>处于
                      <span style="color: #E16978">冻结 - 不可交易</span> 状态</span>
                  </span>

                </p>
              </span>
            </div>
          </div>
          <div class="total">
            <span>你将发送</span>
            <span id="total">0.00</span>
            <span>DC</span>
          </div>
          <!-- <div class="amount data">
          <div>
            <div>数据</div>
            <div>
              <button>添加数据</button>
            </div>
            <div class="show-textarea">
              <div class="add-data">
                <textarea name="addData" id="add-data" cols="50" rows="5">xxx</textarea>
                <div>您可以添加与事务一起发送的额外数据。如果您将“空”字段留空，它将尝试从您的数据中部署一个契约。</div>
              </div>
            </div>
          </div>
        </div>-->
          <div class="select-slider" style="display:none">
            <div>
              <div>选择费用</div>
              <div>
                <div id="fee" class="leftLabel"></div>
                <span>DC</span>
              </div>
            </div>
            <div>
              <div>
                <div class="nstSlider" data-range_min="0" data-range_max="10000" data-cur_min="0">
                  <div class="leftGrip"></div>
                </div>
                <div>
                  <div>更便宜</div>
                  <div>更快</div>
                </div>
              </div>
              <div>
                这是用于处理此事务的最多金额。您的交易将在几秒钟内被开采出来。
              </div>
            </div>
          </div>
          <div class="amount sum">
            <div>
              <div>总额
                <span style="font-size: 14px">(含手续费
                  <span id="fee-rate" style="font-size: 14px">0</span>%)</span>
              </div>
              <div>
                <span id="sum">0.0</span>&nbsp;&nbsp;
                <span>DC</span>
              </div>
            </div>
          </div>
          <br>
          <hr>
          <div class="send">
            <button id="trade">发送信息</button>
          </div>
          <br>
        </div>
        <div id="all-contract-content" class="nano-content">
          <div class="title wallets">
            <p>
              <span>合约</span>
              <span>相关</span>
            </p>
          </div>
          <div style="width: 200px;">
            <div class="add-account">
              <div>
                <i class="fa fa-plus" aria-hidden="true"></i>
              </div>
              <div>部署新的合约</div>
            </div>
          </div>
          <p style="margin-top: 70px;">
            <span class="sign">定制合约</span>
          </p>
          <p class="sign-content">为了观看和合约已经部署在你需要知道它的地址和JSON格式的接口描述blockchain互动。</p>
          <br>
          <div style="width: 150px;">
            <div class="add-account">
              <div>
                <i class="fa fa-plus" aria-hidden="true"></i>
              </div>
              <div>查看合约</div>
            </div>
          </div>
          <p style="margin-top: 70px;">
            <span class="sign">定制 Tokens</span>
          </p>
          <p class="sign-content">令牌是货币和其他种类物建在伊斯利恩平台。为了让帐户监视令牌并发送令牌，您必须将它们的地址添加到列表中。你可以通过简单地修改这个例子自定义标记合约或学习更多关于伊斯利恩令牌创建您自己的标记。</p>
          <br>
          <div style="width: 180px;">
            <div class="add-account">
              <div>
                <i class="fa fa-plus" aria-hidden="true"></i>
              </div>
              <div>查看 Token</div>
            </div>
          </div>
          <br>
        </div>
        <!-- <input id="ipc-test" value="123456" style="display:none;" /> -->
      </div>
    </div>
  </div>
  <div class="window-reload" title="重载页面 ( Ctrl + R )">
    <i class="fa fa-refresh" aria-hidden="true"></i>
  </div>
  <script>
    var ajaxWebsite = require('../modules/website.js');
    // console.log(ajaxWebsite);
    const ipcRenderer = require('electron').ipcRenderer

    // 接受主进程回来的数据 确认权限
    ipcRenderer.send('indexPermission', (event, arg) => {
      console.log('主页去请求是管理员登录还是用户登录')
    })
  </script>
  <script>
    if (typeof module === 'object') {
      window.module = module;
      module = undefined;
    }
  </script>
  <script type="text/javascript" src="../static/js/jquery-1.11.0.min.js"></script>
  <script type="text/javascript" src="../static/js/jquery.nstSlider.min.js"></script>
  <script type="text/javascript" src="../static/js/zeroModal.min.js"></script>
  <!-- <script type="text/javascript" src="../static/js/jquery.nanoscroller.min.js"></script> -->
  <script src="../static/js/navChange.js"></script>
  <script src="../static/js/bignumber.min.js"></script>
  <script src="../static/js/send.js"></script>
  <script src="../static/js/wallets.js"></script>
  <script src="../static/js/clipboard.min.js"></script>
  <script src="../static/js/toast.script.js"></script>
  <script src="../static/js/loading.min.js"></script>
  <script src="../static/js/jquery.animateNumber.min.js"></script>
  <script>
    if (window.module) module = window.module;
  </script>
  <script>
    // You can also require other files to run in this process
    require('../renderer.js');
    var PerfectScrollbar = require('perfect-scrollbar');
    var modalHeight = '330px'

    const ps = new PerfectScrollbar('#container', {
      wheelSpeed: 0.4,
      wheelPropagation: true,
      minScrollbarLength: 30
    });

    ipcRenderer.on('returnPermission', (event, arg) => {
      // console.log(arg + "___")
      var loginAccount = arg['account']
      var code = arg['code']
      var a_email = arg['email']
      var user_name = arg['username']

      // console.log(arg)

      // 获取 币总量、区块节点（带有地址的用户数）、区块高度
      setInterval(function () {
        getFirstData(code)
        // console.clear()
      }, 4500)

      if (code == 3) {
        // 获取所有用户
        setTimeout(function () {
          getAllAccount(code, loginAccount)
        }, 1000)
      } else if (code == 2) {
        // 根据 loginAccount 重新布局 只显示一个人的个人信息
        setInterval(function () {
          getOneAccount(code, loginAccount, a_email, user_name)
        }, 4000)
      }

      if (code == 2) {
        $('#search-account-detail').css('display', 'none')
      }

      $(document).ready(function () {

        if (code == 3) {
          // 导航栏  挖矿启动与停止
          $('#stop').css('display', 'none')
          $('#start').css('display', 'inline')
          $('#preloader_1').css('display', 'none')
          $('#start').click(function () {
            $('#preloader_1').css('display', 'inline-block')
            $.ajax({
              url: ajaxWebsite + 'Home/Ytc/startYTC',
              type: 'GET',
              success: function (res) {
                $('#start').css('display', 'none')
                $('#stop').css('display', 'inline')
                $('.wd-tip').text('挖矿中…')
                // console.log(res)
              }
            })
          })
          $('#stop').click(function () {
            $.ajax({
              url: ajaxWebsite + 'Home/Ytc/stopYTC',
              type: 'GET',
              success: function (res) {
                $('#stop').css('display', 'none')
                $('#start').css('display', 'inline')
                $('#preloader_1').css('display', 'none')
                $('.wd-tip').text('')
                // console.log(res)
              }
            })
          })
        } else {
          $('#stop').css('display', 'none')
          $('#start').css('display', 'none')
          $('#preloader_1').css('display', 'none')
        }

        // 用户输入的数量 处理
        $('#amount').bind('input propertychange', function () {
          if ($('#amount').val() == '') {
            $('#total').text('0.00')
          } else {
            var amount = new BigNumber($('#amount').val())
            // var y = new BigNumber(Math.pow(10, 18))
            // amount = new BigNumber(amount.dividedBy(y))
            // console.log(amount)
            $('#total').text(amount.toFixed(16))
          }
        })

        // 初次加载时 From 输入框 检测余额
        if ($('#from').val() == '') {
          // 超级管理员 账号 （测试）
          if (code == 3) {
            $.ajax({
              url: ajaxWebsite + 'Home/Ytc/getMainAccount',
              type: 'GET',
              success: function (res) {
                var MainAccount = res
                $('#from').val(MainAccount)
                var account = $('#from').val()
                getTotalAmount(account)
                getFreezeAmount(account)
              }
            })
          } else {
            // 登录用户
            $('#from').val(loginAccount)
            $("#from").attr("disabled", true)
            var account = $('#from').val()
            getTotalAmount(account)
            getFreezeAmount(account)
          }
        }

        // From 输入框 检测余额
        $('#from').bind('input propertychange ', function () {
          var account = $('#from').val()
          if (account != '' || account.length >= 42) {
            getTotalAmount(account)
            getFreezeAmount(account)
          }
          // console.log(account)
        })

        // 交易操作检测
        $('#trade').click(function () {
          var from = $('#from').val();
          var to = $('#to').val();
          var fs = from.substr(0, 2)
          var ts = to.substr(0, 2)
          var amount = $('#amount').val()
          var t_amount = $('#t_amount').text().replace(/,/g, '')
          var f_amount = $('#freeze_amount').text().replace(/,/g, '')

          if (from == '' || to == '') {
            addToast('输入错误', '交易地址 不能为空', 'error')
            return false
          } else if (from.length != 42 || to.length != 42 || fs != '0x' || ts != '0x') {
            addToast('输入错误', '交易地址 输入有误', 'error')
            return false
          } else if (from == to) {
            addToast('输入错误', '两个交易地址 不能相同', 'error')
            return false
          } else if (amount == '') {
            addToast('输入错误', '数量 不能为空', 'error')
            return false
          } else if (amount != '') {
            amount = new BigNumber(amount)
            t_amount = new BigNumber(t_amount)
            f_amount = new BigNumber(f_amount)
            // console.log(t_amount)
            if (amount.isGreaterThanOrEqualTo(t_amount.minus(f_amount))) {
              addToast('输入错误', '输入数量 不能超出所拥有的数量', 'error')
              return false
            }

          }
          ytcTrade()
        })

        // 交易记录 分页
        var page = 1
        var limit = 5
        getTradeList(page, limit)

        // 通过邮箱搜索 钱包账号详情
        $('#search-account-detail').keydown(function (event) {
          if (event.keyCode == 13) {
            var val = $('#search-account-detail').val()
            zeroModal.loading(4);
            $.ajax({
              url: ajaxWebsite + 'Home/Ytc/getAccountByEmail',
              type: 'POST',
              data: {
                email: val
              },
              success: function (res) {
                // console.log(res)
                if (res == 0) {
                  closeModal()
                  addToast('查找失败', '此账号不存在', 'error')
                  return false
                }
                var accountName = res.username
                var account = res.account

                var money = new BigNumber(res.number)
                var y = Math.pow(10, 18)
                money = money.dividedBy(y).toFormat(16)
                var setMain
                if (code == 3) {
                  setMain = '<div class="o-set-main">设置为主账号</div>'
                } else {
                  setMain = ''
                }
                var content =
                  '<div class="account-detail-content"><div class="account-detail"><img src="../static/images/avatar.png" alt=""><div><p>' +
                  accountName +
                  '</p><p><i class="fa fa-key" aria-hidden="true"></i> <span id="a-copy" >' +
                  account + '</span></p><p><span>' + money +
                  '</span><span> DC</span></p></div></div><div class="account-operation"><div class="note"><p>注意</p><p>帐户不能显示传入事务，但保持和发送以太。要查看传入的事务，创建一个钱包契约来存储以太。</p></div><div class="a-opera"><div class="o-trade-from" alt="">添加到 From</div><div class="o-trade-to" alt="主帐号转账到该帐号">添加到 To</div><div class="o-copy"  alt="钱包地址复制到剪贴板">复制地址</div></div></div></div>'
                showModal("帐号详情", content, '488px', '280px', true, true, '确认', false, true)

                $('.o-trade-to').click(function () {
                  $('#to').val(account)
                  $('.content>div').css('display', 'none')
                  $('#all-send-content').css('display', 'block')
                  closeModal()
                })
                $('.o-trade-from').click(function () {
                  $('#from').val(account)
                  getTotalAmount(account)
                  getFreezeAmount(account)
                  $('.content>div').css('display', 'none')
                  $('#all-send-content').css('display', 'block')
                  closeModal()
                })
                $('.o-set-main').click(function () {
                  changeMainAccount(account)
                  closeModal()
                })
              }
            })
          }

        })

        // 通过交易区块 查询交易详情
        $('#search-trade').keydown(function (event) {
          if (event.keyCode == 13) {
            var val = $('#search-trade').val()
            // console.log(val)
            zeroModal.loading(4);
            $.ajax({
              url: ajaxWebsite + 'Home/Ytc/directSearchTradeDetail',
              type: 'POST',
              data: {
                string: val
              },
              success: function (res) {

                // console.log(res)
                if (res == 0) {
                  closeModal()
                  addToast('查找失败', '此交易不存在', 'error')
                  return false
                }

                var content = '<div class="trade-block-detail"><div><p>交易 Hash: </p><p>' + res.hash +
                  '</p></div><table><tr><td>时间</td><td>' + res.add_time +
                  '</td></tr><tr><td>数量</td><td><span>' + new BigNumber(res.value).dividedBy(
                    Math.pow(10, 18)).toFormat() +
                  '</span>&nbsp;<span>DC</span></td></tr><tr><td>From</td><td>' + res.from +
                  '</td></tr><tr><td>To</td><td>' + res.to + '</td></tr>' +
                  // '<tr><td>交易费</td><td>' +
                  // new BigNumber(res.fee).dividedBy(Math.pow(10, 18)).toFormat() +
                  // ' DC</td></tr>'
                  // + '<tr><td>Gas used</td><td>' + res.gas + '</td></tr><tr><td>Gas price</td><td>' + res.gasprice + '</td></tr>'

                  '<tr><td>区块</td><td><p>' + res.blocknumber + '</p><p>' + res.blockhash +
                  '</p></td></tr></table></div>'
                showModal('交易区块信息', content, '650px', modalHeight, true, true, '确认', false,
                  true)
              }
            })
          }

        })

        // 钱包合约，功能未开放
        $('#add-account-contract').click(function () {
          addToast('添加钱包合约', '功能暂未开放', 'warning')
        })

        // 查询更多交易记录
        $('.get-more').click(function () {
          page += 1;
          // console.log(page)
          getTradeList(page, limit)
        })

      })
    })


    function getFirstData(code) {
      $.ajax({
        url: ajaxWebsite + 'Home/Ytc/getFeeSellRate',
        type: 'GET',
        success: function (res) {
          $('#fee-rate').text(res)
        }
      })
      if (code == 3) {
        $.ajax({
          url: ajaxWebsite + 'Home/Ytc/checkStatus',
          type: 'GET',
          success: function (res) {
            if (res == '0') {
              $('#stop').css('display', 'none')
              $('#start').css('display', 'inline')
              $('#preloader_1').css('display', 'none')
              $('.wd-tip').text('')
            } else if (res == '1') {
              $('#preloader_1').css('display', 'inline-block')
              $('#start').css('display', 'none')
              $('#stop').css('display', 'inline')
              $('.wd-tip').text('挖矿中…')
            }
          }
        })
      } else {

      }

      $.ajax({
        url: ajaxWebsite + 'Home/Ytc/getBlockNumber',
        type: 'GET',
        success: function (res) {
          if (isNaN(res)) {
            $('#block-number').text('NaN')
          } else {
            $('#block-number').text(res)
          }
        }
      })

      $.ajax({
        url: ajaxWebsite + 'Home/Ytc/getAccountNumber',
        type: 'GET',
        success: function (res) {
          $('#node-count').text(res)
        },
      })

      $.ajax({
        url: ajaxWebsite + 'Home/Ytc/getAllBalance',
        type: 'GET',
        success: function (res) {

          var decimal_places = 2;
          var decimal_factor = decimal_places === 0 ? 1 : Math.pow(10, decimal_places);

          var before = $('#balance').text()
          var res = parseFloat(res[0]).toFixed(2)
          var after = res
          $('#balance').text(res)
          $('#balance')
            .prop('number', before * decimal_factor)
            .animateNumber({
                number: after * decimal_factor,

                numberStep: function (now, tween) {
                  var floored_number = Math.floor(now) / decimal_factor,
                    target = $(tween.elem);

                  if (decimal_places > 0) {
                    // force decimal places even if they are 0
                    floored_number = floored_number.toFixed(decimal_places);
                  }

                  target.text(floored_number);
                }
              },
              4300
            );
        }
      })
    }

    function showModal(title, content, width, height, transition, ok, okTitle, cancel, esc) {
      zeroModal.closeAll()
      zeroModal.show({
        title: title,
        content: content,
        width: width,
        height: height,
        transition: transition,
        ok: ok,
        okTitle: okTitle,
        cancel: cancel,
        esc: esc,
        // overlayClose: true
      })
    }

    function closeModal() {
      zeroModal.closeAll()
    }

    function addToast(title, content, type) {
      $.Toast(title, content, type, {
        stack: true,
        has_icon: true,
        has_close_btn: true,
        fullscreen: false,
        timeout: 2000,
        sticky: false,
        has_progress: true,
        rtl: false,
      });
    }

    // window 最小化 最大化 关闭窗口 重载页面
    $('#minbt').click(function () {
      ipcRenderer.send('window-min');
    })
    $('#maxbt').click(function () {
      ipcRenderer.send('window-max')
    })
    $('#closebt').click(function () {
      ipcRenderer.send('window-close');
    })
    $('.window-reload').click(function () {
      ipcRenderer.send('window-reload')
    })

    // 屏蔽ctrl+shift+I 禁止打开开发者工具 
    // $("body").bind("keydown", function (e) {
    //   e = window.event || e;

    //   if ((event.ctrlKey) && (event.shiftKey) && (event.keyCode == 73)) {
    //     e.keyCode = 0;
    //     return false;
    //   }
    // });
  </script>
</body>

</html>